package com.example.demo.web.controller;

import com.example.demo.bean.BaseFile;
import com.example.demo.service.IBaseFileService;
import com.example.demo.utils.ExcelUtils;
import com.example.demo.utils.Message;
import com.example.demo.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/baseFile")
public class BaseFileController {
    @Autowired
    private IBaseFileService baseFileService;
<<<<<<< HEAD
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "baseFile/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file_name", value = "文件名称", paramType = "query"), })
    public void download(HttpServletResponse response, String file_name) throws Exception {
        List<BaseFile> list = baseFileService.query(file_name);
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "名称", "分类", "下载时间", "别名", "文件大小","文件类型"};
        // 2.2 属性
        String[] fieldList = new String[] {  "file_name", "group_name", "upload_time", "ext_name",
                "file_size","file_type"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();

        for (BaseFile app:list) {
            Map<String, Object> map = new HashMap<>();
            map.put("file_name", app.getFileName());
            map.put("group_name", app.getGroupName());
            map.put("upload_time", app.getUploadTime());
            map.put("ext_name", app.getExtName());
            map.put("file_size", app.getFileSize());
            map.put("file_type", app.getFileType());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "baseFile";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);
=======
>>>>>>> 864a435bc6fe12592c10790881eca6d02e3b87cf












    @GetMapping("query")
    public Message query(String fileName) {
        List<BaseFile> list = baseFileService.query(fileName);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(BaseFile file) {
        baseFileService.saveOrUpdate(file);
        return MessageUtil.success("操作成功");
    }
    @DeleteMapping("deleteById")
    public Message deleteById(@NotNull String id) {
        baseFileService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

}
